package com.zfzs.post.modules.finance.service.impl;

import com.zfzs.post.modules.agent.domain.AgentVerified;
import com.zfzs.post.modules.agent.service.AgentVerifiedService;
import com.zfzs.post.modules.finance.service.BenefitWebService;
import com.zfzs.post.modules.finance.service.vo.BenefitDayWebVO;
import com.zfzs.post.modules.finance.service.vo.BenefitInfoWebVO;
import com.zfzs.post.modules.post.domain.TerminalType;
import com.zfzs.post.modules.post.service.TerminalTypeService;
import com.zfzs.post.modules.transaction.domain.BenefitDay;
import com.zfzs.post.modules.transaction.domain.BenefitInfo;
import com.zfzs.post.modules.transaction.domain.TransactionInfo;
import com.zfzs.post.modules.transaction.service.BenefitDayService;
import com.zfzs.post.modules.transaction.service.BenefitInfoService;
import com.zfzs.post.modules.transaction.service.dto.BenefitDayQueryCriteria;
import com.zfzs.post.utils.CloneUtils;
import com.zfzs.post.utils.DateStrUtils;
import com.zfzs.post.utils.SecurityUtils;
import com.zfzs.post.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wq
 * @date 2019/11/27 20:14
 * @description
 */
@Service
public class BenefitWebServiceImpl implements BenefitWebService {


    @Autowired
    private BenefitDayService benefitDayService;

    @Autowired
    private BenefitInfoService benefitInfoService;

    @Autowired
    private AgentVerifiedService agentVerifiedService;

    @Autowired
    private TerminalTypeService terminalTypeService;


    @Override
    public void addTeamBenefitDay(TransactionInfo info, BigDecimal benefit, Long benefitAgentId) {

        BenefitDay benefitDay = benefitDayService.findByAgentIdAndDate(benefitAgentId, DateStrUtils.formatDate(info.getTransactionTime(), DateStrUtils.Y_M_D));

        Long benefitId;

        if (benefitDay == null) {
            benefitDay = new BenefitDay();
            benefitDay.setAgentId(benefitAgentId);
            benefitDay.setBenefitDate(info.getTransactionTime());
            benefitDay.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            benefitDay.setAllBenefit(benefit);
            benefitDay.setAllAmount(info.getAmount());
            benefitDay.setSelfAmount(new BigDecimal("0.00"));
            benefitDay.setSelfBenefit(new BigDecimal("0.00"));
            benefitDay.setTeamAmount(info.getAmount());
            benefitDay.setTeamBenefit(benefit);
            benefitDay.setChannelId(info.getChannelId());
            benefitId= benefitDayService.create(benefitDay).getBenefitId();

        } else {
            benefitDay.setAllAmount(benefitDay.getAllAmount().add(info.getAmount()));
            benefitDay.setAllBenefit(benefitDay.getAllBenefit().add(benefit));

            benefitDay.setTeamAmount(benefitDay.getTeamAmount().add(info.getAmount()));
            benefitDay.setTeamBenefit(benefitDay.getTeamBenefit().add(benefit));

            benefitDayService.update(benefitDay);
            benefitId=benefitDay.getBenefitId();
        }


        addTeamBenefitInfo(benefitId, info, benefit);

    }

    private void addTeamBenefitInfo(Long benefitId, TransactionInfo info, BigDecimal benefit) {

        BenefitInfo benefitInfo = benefitInfoService.findByBenefitId(benefitId, info.getTerminalTypeId());

        BigDecimal allInfoAmount = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getAllInfoAmount();
        BigDecimal teamInfoAmount = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getTeamInfoAmount();
        BigDecimal teamInfoTran = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getTeamInfoTran();
        BigDecimal allInfoTran = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getAllInfoTran();

        if (benefitInfo == null) {
            benefitInfo = new BenefitInfo();
            benefitInfo.setSelfInfoTran(new BigDecimal("0"));
            benefitInfo.setSelfInfoAmount(new BigDecimal("0"));
        }
        benefitInfo.setAllInfoTran(allInfoTran.add(info.getAmount()));
        benefitInfo.setAllInfoAmount(allInfoAmount.add(benefit));
        benefitInfo.setTeamInfoAmount(teamInfoAmount.add(benefit));
        benefitInfo.setTeamInfoTran(teamInfoTran.add(info.getAmount()));
        benefitInfo.setBenefitId(benefitId);
        benefitInfo.setTerminalTypeId(info.getTerminalTypeId());
        benefitInfoService.create(benefitInfo);


    }

    @Override
    public void addSelfBenefitDay(TransactionInfo info, BigDecimal benefit) {


        BenefitDay benefitDay = benefitDayService.findByAgentIdAndDate(info.getAgentId(), DateStrUtils.formatDate(info.getTransactionTime(), DateStrUtils.Y_M_D));

        Long benefitId;
        if (benefitDay == null) {
            benefitDay = new BenefitDay();
            benefitDay.setAgentId(info.getAgentId());
            benefitDay.setBenefitDate(info.getTransactionTime());
            benefitDay.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
            benefitDay.setAllBenefit(benefit);
            benefitDay.setAllAmount(info.getAmount());
            benefitDay.setSelfAmount(info.getAmount());
            benefitDay.setSelfBenefit(benefit);
            benefitDay.setTeamAmount(new BigDecimal("0"));
            benefitDay.setTeamBenefit(new BigDecimal("0"));
            benefitDay.setChannelId(info.getChannelId());
            benefitId = benefitDayService.create(benefitDay).getBenefitId();

        } else {
            benefitDay.setAllAmount(benefitDay.getAllAmount().add(info.getAmount()));
            benefitDay.setAllBenefit(benefitDay.getAllBenefit().add(benefit));

            benefitDay.setSelfAmount(benefitDay.getSelfAmount().add(info.getAmount()));
            benefitDay.setSelfBenefit(benefitDay.getSelfBenefit().add(benefit));

            benefitDayService.update(benefitDay);

            benefitId=benefitDay.getBenefitId();
        }

        addSelfBenefitInfo(benefitId, info, benefit);

    }



    @Override
    public void addSelfBenefitInfo(Long benefitId, TransactionInfo info, BigDecimal benefit) {
        BenefitInfo benefitInfo = benefitInfoService.findByBenefitId(benefitId, info.getTerminalTypeId());

        BigDecimal allInfoAmount = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getAllInfoAmount();
        BigDecimal selfInfoAmount = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getSelfInfoAmount();
        BigDecimal selfInfoTran = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getSelfInfoTran();
        BigDecimal allInfoTran = benefitInfo == null ? new BigDecimal("0.00") : benefitInfo.getAllInfoTran();

        if (benefitInfo == null) {
            benefitInfo = new BenefitInfo();
            benefitInfo.setTeamInfoTran(new BigDecimal("0"));
            benefitInfo.setTeamInfoAmount(new BigDecimal("0"));
        }

        benefitInfo.setAllInfoAmount(allInfoAmount.add(benefit));
        benefitInfo.setSelfInfoAmount(selfInfoAmount.add(benefit));
        benefitInfo.setAllInfoTran(allInfoTran.add(info.getAmount()));
        benefitInfo.setSelfInfoTran(selfInfoTran.add(info.getAmount()));
        benefitInfo.setBenefitId(benefitId);
        benefitInfo.setTerminalTypeId(info.getTerminalTypeId());
        benefitInfoService.create(benefitInfo);
    }



    @Override
    public Map<String, Object> getBenefitPage(BenefitDayQueryCriteria criteria, Integer currPage, Integer size) {
             //获取渠道ID
        Long id = SecurityUtils.getUserId();

        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }

        if(StringUtils.isNotBlank(criteria.getName())){
            List<AgentVerified> agents=agentVerifiedService.findByName(criteria.getName());

            List<Long> agentIds=agents.stream().map(AgentVerified::getId).collect(Collectors.toList());
            criteria.setAgentIds(agentIds);
        }

        Page<BenefitDay> page = benefitDayService.getBenefitPage(criteria, currPage, size);

        List<BenefitDay> content = page.getContent();

        List<BenefitDayWebVO> list= new ArrayList();

        for (BenefitDay benefitDay : content) {

            AgentVerified agent = agentVerifiedService.findByAgentId(benefitDay.getAgentId());

            BenefitDayWebVO v= CloneUtils.clone(benefitDay,BenefitDayWebVO.class);

            v.setName(agent.getCardName());

            List<BenefitInfo> infoList= benefitInfoService.findByBenefitId(benefitDay.getBenefitId());

            List<BenefitInfoWebVO> webVOList=new ArrayList<>();

            for (BenefitInfo benefitInfo : infoList) {

                TerminalType terminalType = terminalTypeService.findById(benefitInfo.getTerminalTypeId());

                BenefitInfoWebVO webInfo=CloneUtils.clone(benefitInfo,BenefitInfoWebVO.class);

                webInfo.setTerminalName(terminalType.getTerminalName());

                webVOList.add(webInfo);

            }
            v.setInfoList(webVOList);
            list.add(v);
        }

        Map<String,Object> map=new HashMap<>(2);
        map.put("content", list);
        map.put("totalElements", page.getTotalElements());
        return map;
    }

}
